﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Threading;


namespace Jenvin.Library.Threading
{
    class TaskDemo
    {
        public class DownUrlTask : AbstractTask
        {
            public string Msg { get; set; }
            public override void Execute()
            {
                Thread.Sleep(1000);
                Console.WriteLine(Msg);
            }
        }


        public class CountTask : AbstractTask
        {
            public int Count { get; set; }

            public override void Execute()
            {
                if (Count == -1)
                {
                    this.Context.Exited = true;
                    Console.WriteLine(this.Context.Params);
                }
                this.Context.Params = (int)this.Context.Params + Count;
            }
        }

        public class DataTask : AbstractTask
        {
            public int PageIndex { get; set; }
            public override void Execute()
            {
                Console.WriteLine(string.Format("get {0} page data from db", PageIndex));
            }
        }

        public class DataTaskList : IEnumerable<DataTask>
        {
            protected int PageCount { get; set; }
            public DataTaskList(int pageCount)
            {
                this.PageCount = pageCount;
            }
            public IEnumerator<DataTask> GetEnumerator()
            {
                if (PageCount <= 0)
                    yield break;

                int ptr = 0;
                while (true)
                {
                    if (ptr > PageCount)
                        yield break;
                    else
                        yield return new DataTask() { PageIndex = ptr };
                    ptr++;
                }
                
            }
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }


        static void Main(string[] args)
        {
            //TaskContext context = new TaskContext();

            //TaskActor actor = new TaskActor(context);
            //actor.Post(new DownUrlTask() { Msg = "1" });
            //actor.Post(new DownUrlTask() { Msg = "2" });
            //actor.Post(new DownUrlTask() { Msg = "3" });
            //actor.Post(new ExitTask());

            //TaskContext context1 = new TaskContext();
            //TaskActor actor1 = new TaskActor(context1);
            //actor1.Post(new DownUrlTask() { Msg = "4" });
            //actor1.Post(new DownUrlTask() { Msg = "5" });
            //actor1.Post(new DownUrlTask() { Msg = "6" });
            //actor1.Post(new ExitTask());

            //context.WaitForExit();
            //context1.WaitForExit();

            //TaskContext context2 = new TaskContext();
            //context2.Params = 0;
            //TaskActor actor2 = new TaskActor(context2);
            //for (int i = 0; i < 10000; i++)
            //{
            //    actor2.Post(new CountTask() { Count = i });
            //}
            //actor2.Post(new CountTask() { Count = -1 });
            //context2.WaitForExit();

            TaskContext context3 = new TaskContext();
            using (TaskQueue queue = new TaskQueue(10, context3))
            {
                foreach (DataTask task in new DataTaskList(100))
                {
                    queue.EnqueueTask(task);
                }
               
            }
            // don't use waitForExit() in taskQueue ,bacuase using statement auto wait all thread exit
            //context3.WaitForExit();
        }



    }
}
